const commonUrl = "https://miaohuatang.huimaiapp.cn/"
const imageUrlAgain = "https://miaohuatang.huimaiapp.cn"

function postRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var that = this;
		var postData = data;
		uni.request({
			url: commonUrl + url,
			data: postData,
			method: "POST",
			header: {
				"content-type": "application/x-www-form-urlencoded",
			},
			success: function(res) {
				if (res.data.code == 200) {
					resolve(res.data);
					
				} else {
					if(res.data.code=='300'){
						uni.setStorageSync('openid','');
						uni.showToast({
							title:'登录已过期,请重新登录',
							icon:'none'
						})
						return false;
					}
					resolve(res.data);
				}
			},
			error: function(e) {
				reject("网络出错");
			}
		});
	});
	return promise;
}

function getRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var that = this;
		var postData = data;
		uni.request({
			url: commonUrl + url,
			data: postData,
			method: "GET",
			dataType: "json",
			header: {
				"content-type": "application/json"
			},
			success: function(res) {
				console.log(res.statusCode)
				if (res.data.code == 200) {
					resolve(res.data);
				} else {
					resolve(res.data.msg);
				}
			},
			error: function(e) {
				reject("网络出错");
			}
		});
	});
	return promise;
}

module.exports = {
	post: postRequest,
	get: getRequest,
	imgUrl: imageUrlAgain
}
